package com.laborder.sysyy.controller.student;

import com.laborder.sysyy.entity.*;
import com.laborder.sysyy.service.classes.ClassService;
import com.laborder.sysyy.service.college.CollegeService;
import com.laborder.sysyy.service.course.CourseService;
import com.laborder.sysyy.service.student.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "student")
public class StudentController {
	@Autowired
	private StudentService studentService;
	@Autowired
	private CollegeService collegeService;
	@Autowired
	private ClassService classService;
	@Autowired
	private CourseService courseService;


	@GetMapping("toStudent")
	public String toStudent(Model model){

		List<Course> courseList = courseService.findAllCourse();
		List<College> collegeList = collegeService.findAllCollege();
		List<Classes> classList = classService.findAllClasses();
		model.addAttribute("courseList",courseList);
		model.addAttribute("classList",classList);
		model.addAttribute("collegeList",collegeList);
		List<College> c = collegeService.selectCollege();
		model.addAttribute("collegeList",c);
		return "/student/studentList";
	}
	 /**
	   * @function: 学生分页查询
	   * @author: 759057893@qq.com lyz
	   * @date: 2019/1/9 13:15
	   */
	@ResponseBody
	@RequestMapping("/findStudentWithPage")
	public Map<String,Object> findStudentWithPage(int page,int limit,String sName,
												  String sClassNum,String cName,
												  String caName,String clName){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("page",(page-1)*limit);
		map.put("limit",limit);
		map.put("sName",sName);
		map.put("sClassNum",sClassNum);
		map.put("cName",cName);
		map.put("caName",caName);
		map.put("clName",clName);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code",0);
		resultMap.put("msg","");
		resultMap.put("count",studentService.getStudentCount(map));
		resultMap.put("data",studentService.findStudentWithPage(map));
		return  resultMap;
	}

	/**
	 * @function: 删除学生信息
	 * @author: 759057893@qq.com lyz
	 * @date: 2019/1/10 14:12
	 */
	@ResponseBody
	@RequestMapping("/delStudentInfo")
	public  boolean delStudentInfo(int sId) {
		boolean flag = false;
		if(!flag){
			flag = studentService.delStudentInfo(sId);
		}
		return flag;
	}


	/**
	 * @function: 批量删除学生信息
	 * @author: 759057893@qq.com lyz
	 * @date: 2019/1/10 14:12
	 */
	@ResponseBody
	@RequestMapping("/delManyStudentInfo")
	public  boolean delManyStudentInfo( @RequestParam("sId[]") int sId[]  ) {
		boolean flag = false;
		for(Integer sIds : sId){
			flag  = studentService.delManyStudentInfo(sIds);
		}
		return flag;
	}

	@GetMapping("toAddStudent")
	public String toAddStudent(Model model){
		List<Course> courseList = courseService.findAllCourse();
		List<College> collegeList = collegeService.findAllCollege();
		List<Classes> classList = classService.findAllClasses();
		model.addAttribute("courseList",courseList);
		model.addAttribute("classList",classList);
		model.addAttribute("collegeList",collegeList);
		return "/student/studentAdd";
	}
	/**
	 * @function: 添加学生信息
	 * @author: 759057893@qq.com lyz
	 * @date: 2019/1/9 21:42
	 */
	@ResponseBody
	@RequestMapping("saveStudentInfo")
	public  int saveStudentInfo(Student student) {
		int flag = 0;
		Student s =studentService.getStudentInfoByName(student.getsClassNum());
		if(s!=null){
			flag = -1;
		}else if(s==null){
			flag = studentService.saveStudentInfo(student);
		}else{
			flag = 0;
		}
		return flag;
	}

	/**
	 * @function:  学生信息查看
	 * @author: 759057893@qq.com lyz
	 * @date: 2019/1/10 14:45
	 */
	@RequestMapping("studentDetail")
	public String studentDetail(Model model,int sId) {
		Student student = studentService.selectStudentById(sId);
		model.addAttribute("student", student);
		List<Course> courseList = courseService.findAllCourse();
		List<College> collegeList = collegeService.findAllCollege();
		List<Classes> classList = classService.findAllClasses();
		model.addAttribute("courseList",courseList);
		model.addAttribute("classList",classList);
		model.addAttribute("collegeList",collegeList);
		return "student/studentDetail";
	}

	/**
	 * @function:  学生信息修改前回显信息
	 * @author: 759057893@qq.com lyz
	 * @date: 2019/1/10 14:45
	 */
	@RequestMapping("editStudent")
	public  String editStudent(Model model,int sId){
		Student student = studentService.selectStudentById(sId);
		model.addAttribute("student", student);
		List<Course> courseList = courseService.findAllCourse();
		List<College> collegeList = collegeService.findAllCollege();
		List<Classes> classList = classService.findAllClasses();
		model.addAttribute("courseList",courseList);
		model.addAttribute("classList",classList);
		model.addAttribute("collegeList",collegeList);
		return "student/editStudent";
	}

	/**
	 * @function: 修改学生数据
	 * @author: 759057893@qq.com lyz
	 * @date: 2019/1/10 16:00
	 */
	@ResponseBody
	@RequestMapping("/updateStudentInfo")
	public  boolean updateStudentInfo(Student student) {

		return studentService.updateStudentInfo(student);
	}



}